public class LengthOfLongestSubstring {
    // leetcode 无重复字符的最长字串
    // https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/?envType=study-plan-v2&envId=top-100-liked
    public int lengthOfLongestSubstring(String s) {
        int len = s.length();
        if (len == 0) return 0;
        int left = 0, right = 0;
        int[] hash = new int[128];
        int maxLength = 0;
        while (right < len) {
            while (right < len && ++hash[s.charAt(right)] == 1) {
                right++;
            }
            maxLength = Math.max(maxLength, right-left);
            while (left < right && --hash[s.charAt(left)] != 1) {
                left++;
            }
            left++;
            right++;
        }
        return maxLength;
    }

    public static void main(String[] args) {
        System.out.println(new LengthOfLongestSubstring().lengthOfLongestSubstring("abcabcbb"));
    }
}
